package org.bt.auth.controller;

import cn.dev33.satoken.stp.StpLogic;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bt.auth.entity.User;
import org.bt.auth.service.UserServiceImpl;
import org.bt.auth.vo.UserVo;
import org.bt.common.constant.CommonConstant;
import org.bt.common.entity.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zwj
 * @date 2022/12/9
 * @description TODO 用户管理
 */
@RestController
@RequestMapping(value = "/user", headers = {CommonConstant.ApiVersion.VERSION_V1})
@Api(tags = "用户管理")
public class UserController {

    @Autowired
    private UserServiceImpl userService;

    @PostMapping
    @ApiOperation(value = "新增用户")
    public Response<?> add(@RequestBody User user) {
        try {
            userService.add(user);
            return new Response<>().ok("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Response<>().error(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "修改用户信息")
    public Response<?> update(@RequestBody UserVo userVo) {
        try {
            userService.update(userVo);
            return new Response<>().ok("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Response<>().error(e.getMessage());
        }
    }

    @GetMapping
    @ApiOperation(value = "查询用户")
    public Response<?> get() {
        try {
            StpLogic stpLogic = StpUtil.getStpLogic();
            System.out.println(stpLogic.getExtra("username") + "--------------------");
            return new Response<>().data(userService.list(), "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Response<>().error(e.getMessage());
        }
    }
}
